package com.rdgk.newtech.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.dao.StopwordDao;
import com.rdgk.newtech.service.StopwordService;
import com.rdgk.newtech.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: msun
 */
@Service
public class StopwordServiceImpl implements StopwordService {
    @Autowired
    private StopwordDao stopwordDao;
    private int is_deleted = 0;
    private int StopwordId = -1;

    /**
     * 显示停用词列表
     */
    @Override
    public JSONObject listStopword(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        System.out.println("jsonObject:"+jsonObject);
        int count = stopwordDao.countStopword(jsonObject);
        System.out.println("count:"+count);
        List<JSONObject> list = stopwordDao.listStopword(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 搜索停用词列表项
     */
    @Override
    public JSONObject listStopwordBySearch(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        System.out.println("jsonObject:"+jsonObject);
        int count = stopwordDao.countStopwordtBySearch(jsonObject);
        System.out.println("count:"+count);
        List<JSONObject> list = stopwordDao.listStopwordBySearch(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 按名称查询停用词是否存在
     */
    @Override
    public JSONObject getStopwordCountByName(JSONObject jsonObject) {
        List<JSONObject> list= stopwordDao.getStopwordCountByName(jsonObject);
        if (list.size() >= 1) {
            is_deleted = list.get(0).getIntValue("is_deleted");
            if (is_deleted == 0){
                return CommonUtil.successJson("exist");
            }else{
                StopwordId = list.get(0).getIntValue("id");
            }
        } else {
            return CommonUtil.successJson("nonexist");
        }
        return CommonUtil.successJson("nonexist");
    }

    /**
     * 新增停用词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject addStopword(JSONObject jsonObject) {
        Date now = new Date();
        jsonObject.put("createAt", now);
        jsonObject.put("updateAt", now);
        if (is_deleted == 0){
            stopwordDao.addStopword(jsonObject);
        }else {
            jsonObject.put("StopwordId",StopwordId);
            stopwordDao.updateStopword(jsonObject);
        }
        return CommonUtil.successJson();
    }

    /**
     * 更新停用词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject updateStopword(JSONObject jsonObject) {
        Date now = new Date();
        jsonObject.put("createAt", now);
        jsonObject.put("updateAt", now);
        stopwordDao.updateStopword(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 删除停用词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deleteStopword(JSONObject jsonObject) {
        Date now = new Date();
        jsonObject.put("deleteAt", now);
        stopwordDao.deleteStopword(jsonObject);
        return CommonUtil.successJson();
    }
    
}
